package com.example.luckin.slice.fraction;

import com.example.luckin.ResourceTable;
import com.example.luckin.slice.entity.Category;
import com.example.luckin.slice.entity.Item;
import com.example.luckin.slice.provider.CategoryProvider;
import com.example.luckin.slice.provider.ItemProvider;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.ToastDialog;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import java.util.*;


public class MenuFraction extends Fraction {
    private Component component;
    private Text txtTilte;
    private ListContainer listCategory;
    private ListContainer listDetail;
    private CategoryProvider categoryProvider;
    private ItemProvider itemProvider;

    private PageSlider pageSlider;
    private PageSliderIndicator pageSliderIndicator;
    // 定义线程
    private EventHandler mHandler = new EventHandler(EventRunner.create());

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            int mCurrentItem = pageSlider.getCurrentPage();
            mCurrentItem++;
            if (mCurrentItem >= 2) {
                mCurrentItem = 0;
            }
            int finalMCurrentItem = mCurrentItem;
            getFractionAbility().getContext().getUITaskDispatcher().asyncDispatch(() -> {
                pageSlider.setCurrentPage(finalMCurrentItem);
                mHandler.postTask(runnable, 3000);
            });
        }
    };

    private void initCarousel() {

        pageSlider = (PageSlider) component.findComponentById(ResourceTable.Id_ps_menu_adv);

        List<Component> list = new ArrayList<>(0);

        Image image = new Image(getFractionAbility().getContext());
        image.setWidth(DependentLayout.LayoutConfig.MATCH_PARENT);
        image.setHeight(DependentLayout.LayoutConfig.MATCH_PARENT);
        image.setPixelMap(ResourceTable.Media_menu_adv01);
        image.setScaleMode(Image.ScaleMode.STRETCH);
        list.add(image);

        Image image2 = new Image(getFractionAbility().getContext());
        image2.setWidth(DependentLayout.LayoutConfig.MATCH_PARENT);
        image2.setHeight(DependentLayout.LayoutConfig.MATCH_PARENT);
        image2.setPixelMap(ResourceTable.Media_menu_adv02);
        image2.setScaleMode(Image.ScaleMode.STRETCH);
        list.add(image2);

        pageSlider.setProvider(new PageSliderProvider() {
            @Override
            public int getCount() {
                return list.size();
            }

            @Override
            public Object createPageInContainer(ComponentContainer componentContainer, int i) {
                Component component = list.get(i);
                componentContainer.addComponent(component);
                return component;
            }

            @Override
            public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
                componentContainer.removeComponent(list.get(i));
            }

            @Override
            public boolean isPageMatchToObject(Component component, Object o) {
                return component == o;
            }
        });

        pageSliderIndicator = (PageSliderIndicator) component.findComponentById(ResourceTable.Id_psi_menu_adv);
        pageSliderIndicator.setViewPager(pageSlider);
        mHandler.postTask(runnable, 3000);
    }

    @Override
    protected void onComponentDetach() {
        super.onComponentDetach();
        mHandler.removeTask(runnable);
    }

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        component = scatter.parse(ResourceTable.Layout_fraction_menu, container, false);
        return component;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        initComponent();
        initCarousel();
    }

    private void initComponent() {
        txtTilte = (Text) component.findComponentById(ResourceTable.Id_txt_menu_title);
        listCategory = (ListContainer) component.findComponentById(ResourceTable.Id_list_category);
        listDetail = (ListContainer) component.findComponentById(ResourceTable.Id_list_detail);
        List<Category> lists = new ArrayList<>(0);
        lists.add(new Category("人气Top"));
        lists.add(new Category("大师咖啡"));
        lists.add(new Category("零度拿铁"));
        lists.add(new Category("瑞纳冰"));
        lists.add(new Category("经典饮品", "66折优惠"));
        lists.add(new Category("健康轻食"));
        lists.add(new Category("鲜榨果蔬汁"));

        List<Item> listItems = new ArrayList<>();
        listItems.add(new Item("大师咖啡", "咖啡", "一糖一奶", "￥24", "充2赠1", ResourceTable.Media_coffee_item, "人气Top"));
        listItems.add(new Item("大师咖啡", "咖啡", "一糖一奶", "￥24", "充2赠1", ResourceTable.Media_coffee_item, "人气Top"));
        listItems.add(new Item("大师咖啡", "咖啡", "一糖一奶", "￥24", "充2赠1", ResourceTable.Media_coffee_item, "大师咖啡"));
        listItems.add(new Item("大师咖啡", "咖啡", "一糖一奶", "￥24", "充2赠1", ResourceTable.Media_coffee_item, "大师咖啡"));
        listItems.add(new Item("大师咖啡", "咖啡", "一糖一奶", "￥24", "充2赠1", ResourceTable.Media_coffee_item, "零度拿铁"));
        listItems.add(new Item("大师咖啡", "咖啡", "一糖一奶", "￥24", "充2赠1", ResourceTable.Media_coffee_item, "零度拿铁"));
        listItems.add(new Item("大师咖啡", "咖啡", "一糖一奶", "￥24", "充2赠1", ResourceTable.Media_coffee_item, "瑞纳冰"));
        listItems.add(new Item("大师咖啡", "咖啡", "一糖一奶", "￥24", "充2赠1", ResourceTable.Media_coffee_item, "瑞纳冰"));

        categoryProvider = new CategoryProvider(this.getFractionAbility().getContext(), lists);
        listCategory.setItemProvider(categoryProvider);

        listDetail.setBoundarySwitch(true);
        listDetail.setBoundaryColor(new Color(Color.rgb(242, 242, 242)));
        listCategory.setItemClickedListener((listContainer, component, index, l) -> {
            categoryProvider.setSelectItem(index);
            categoryProvider.notifyDataChanged();
            String name = lists.get(index).getName();
            for (int i = 0; i < listItems.size(); i++) {
                if (listItems.get(i).getCategory().equals(name)) {
                    listDetail.scrollTo(i);
                    break;
                }
            }
        });

        itemProvider = new ItemProvider(this.getFractionAbility().getContext(), listItems);
        listDetail.setItemProvider(itemProvider);

        listDetail.setScrollListener(() -> {
            int index = listDetail.getItemPosByVisibleIndex(0);
            if (index < 0) return;
            String category = listItems.get(index).getCategory();
            for (int i = 0; i < lists.size(); i++) {
                if (lists.get(i).getName().equals(category)) {
                    categoryProvider.setSelectItem(i);
                    categoryProvider.notifyDataChanged();
                    break;
                }
            }
        });
        listDetail.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {

                new ToastDialog(MenuFraction.this.getFractionAbility().getContext()).setText("Hello "+i).show();
            }
        });
    }
}
